package LeetCode.stack_queue;

import java.util.Stack;

public class LC_20 {
    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        char[] chars = s.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            if(chars[i] == '{' || chars[i] == '[' || chars[i] == '('){
                stack.push(chars[i]);
            } else {
                if(stack.empty()){
                    return false;
                }
                char c1 = stack.peek();
                if((c1 == '[' && chars[i] == ']') ||
                   (c1 == '{' && chars[i] == '}') ||
                   (c1 == '(' && chars[i] == ')')){
                    stack.pop();
                } else {
                    return false;
                }
            }

        }
        return stack.empty();
    }
}
